﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.CarKpiValid;
using S4Store.Service.Valid.StoreValid;
using S4Store.Domain.CarKpi;
using S4Store.Service.Valid.CarKpiValid;
using S4Store.Domain.FinancialKpi;
using S4Store.Service.Valid.FinancialKpiValid;

namespace S4Store.Service
{
    public class FinancialKpiService : BaseService
    {

        public FinancialMonthConfigSave FinancialMonthConfigSave { get; set; }

        public IQueryable<FinancialMonthConfig> GetFinancialMonthConfigQuery()
        {
            return this.GetQuery<FinancialMonthConfig>();
        }
        public IQueryable<FinancialCarBrandMonthConfig> GetFinancialCarBrandMonthConfigQuery()
        {
            return this.GetQuery<FinancialCarBrandMonthConfig>();
        }
        public IQueryable<FinancialDayData> GetFinancialDayDataQuery()
        {
            return this.GetQuery<FinancialDayData>();
        }

        public IQueryable<CarBrand> GetCarbrandsForStoreMonth(int year, int month,int storeid)
        {
            var query = this.GetQuery<FinancialCarBrandMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month.Year == year &&
                               q.Month.Month == month
                         select q.CarBrand;
            return result.Distinct();
        }

        public IQueryable<CarBrand> GetCarbrandsForStoreMonths(DateTime start, DateTime end, int storeid)
        {
            DateTime s = new DateTime(start.Year, start.Month, 1);
            DateTime e = new DateTime(end.Year, end.Month, 1).MonthLastOrNow();
            e = new DateTime(e.Year, e.Month, e.Day, 23, 59, 59);
            var query = this.GetQuery<FinancialCarBrandMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month>=s &&
                               q.Month<=e
                         select q.CarBrand;
            return result.Distinct();
        }

        public FinancialMonthConfig GetFinancialMonthConfig(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                FinancialMonthConfig config = this.GetFinancialMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
                if (config == null)
                {
                    config = new FinancialMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
            else
            {
                FinancialMonthConfig config = this.GetFinancialMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
                if (config == null )
                {
                    config = new FinancialMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
        }

        public void SubmitFinancialKpiMonthConfig(FinancialMonthConfig config)
        {
            this.Valid(config, FinancialMonthConfigSave);
            if (config.Id == 0)
            {
                this.GetSet<FinancialMonthConfig>().Add(config);
            }
            
        }

        public void SubmitFinancialCarBrandMonthConfig(int year, int month, int storeid, List<FinancialCarBrandMonthConfig> configs)
        {
            var FinancialCarBrandMonthConfigs = GetFinancialCarBrandMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            foreach (var Carc in FinancialCarBrandMonthConfigs)
            {
                this.GetSet<FinancialCarBrandMonthConfig>().Remove(Carc);
            }
            foreach (var config in configs)
            {
                this.GetSet<FinancialCarBrandMonthConfig>().Add(config);
            }
        }

        public IQueryable<FinancialCarBrandMonthConfig> GetFinancialCarBrandMonthConfig(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetFinancialCarBrandMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetFinancialCarBrandMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            }
        }

        public IQueryable<FinancialDayData> GetFinancialDayDatas(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetFinancialDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetFinancialDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid);
            }
        }


        public IQueryable<FinancialDayData> GetFinancialDayDatas(int year, int month, int day, int storeid)
        {
            return GetFinancialDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
        }

        public void SubmitFinancialDayDatas(int year, int month, int day, int storeid, List<FinancialDayData> days)
        {
            var carbrandsinplan = this.GetCarbrandsForStoreMonth(year, month, storeid).ToList();

            if (days.Any(t => carbrandsinplan.All(x => x.Id != t.CarBrandId)))
            {
                throw new Exception("提交的数据里包含未做月初计划的车型");
            }

            var datas = GetFinancialDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
            foreach (var data in datas)
            {
                this.GetSet<FinancialDayData>().Remove(data);
            }
            foreach (var config in days)
            {
                this.GetSet<FinancialDayData>().Add(config);
            }
        }
    }
}
